import VueRouter from "vue-router";
import IndexPage from "../components/IndexPage.vue";
import InfoPage from "../components/InfoPage.vue";
import RecordPage from "../components/info/RecordPage.vue";
import axios from "axios";

const router = new VueRouter({
    routes:[
        {
            path:"/",
            component:IndexPage
        },
        {
            path:"/info",
            name:"info",
            component:InfoPage,
            meta: {
                isAuth: true
            }
        },
        {
            path:"/record",
            component:RecordPage,
            meta: {
                isAuth: true
            }
        },
        {
            path:"/login",
            name:"login",
            component:()=>import("../components/LoginPage.vue"),
            meta: {
                isAuth: true
            }
        },
        {
            path:"/regist",
            component:()=>import("../components/RegistPage.vue")
        }
      
     
    ]
});

router.beforeEach((to, from, next) => {
    
    console.log("进入检查")
    if (to.matched.some(itme => itme.meta.isAuth)) {   //当前路由是否需要权限控制

        console.log("需要权限控制")
         //清除所有cookie

        if(document.cookie.split(';')[0].split('=')[1]==""||document.cookie.split(';')[0].split('=')[1]==undefined){
        
            console.log("cookie为空,将清除本地token")
            localStorage.removeItem('name')
            localStorage.removeItem('uid')
            localStorage.removeItem('token')
        }
        const token = localStorage.getItem('token');
        const isLogin = !!token;

        if (isLogin) { //本地有token
            //向后端发送token验证
            let token1 = { token: token}
            
            axios.post('/verify',JSON.stringify(token1))
                .then(res => {
                    
                    if (res.data.status == 'Y') {
                        localStorage.setItem('uid', res.data.uid);//保存用户id
                        //localStorage.setItem('name', res.data.data.name);//保存用户名
                    }
                    console.log(res.data);
                    if (res.data.status == 'N') {//无效token
                        console.log("无效token,去登录页")
                        localStorage.removeItem('token');
                        localStorage.removeItem('uid');
                        localStorage.removeItem('name');
                        router.push('login');
                        
                        return;
                    }
                    if (to.name == 'login') {//已登录，但是要去登录页，重定向到首页
                        console.log("已登录，但是要去登录页，重定向到首页")
                        router.push('/')
                        return;
                    } else {//放行
                        console.log("已登录，放行")
                        next();
                        return;
                    }
          

                })

        }
        if (!isLogin && to.name === 'login') {  //未登录，但是要去登录页
            console.log("未登录，但是要去登录页")
            next()
            return
        }
        console.log(to.name);
        if (!isLogin && to.name !== 'login') { //未登录，去的也不是登录页，重定向到登录页
            console.log("未登录，去的也不是登录页，重定向到登录页")
            router.push('login')
            return
        }
    }
    else {//不需要权限控制，直接放行
        console.log("不需要权限控制，直接放行")
        next();
        return
    }
})

export default router;